package cc.vv.mvp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cc.vv.baselibrary.util.BCObjectProcessingUtil;
import cc.vv.baselibrary.util.LKLogUtil;
import cc.vv.mvp.adapter.holder.BaseViewHolder;
import cc.vv.mvp.binder.BaseDataBinder;
import cc.vv.mvp.delegate.BaseAdapterDelegate;
import cc.vv.mvp.delegate.XDelegate;

/**
 * MVP模式的公共适配器基类（RecyclerView）
 * Created by sun.li on 2018/9/25.
 *
 * @author sun.li
 */

public abstract class BaseRecyclerViewLibAdapter<T extends BaseAdapterDelegate,D extends Object> extends RecyclerView.Adapter<BaseViewHolder>{

    private List<D> data;

    private Context context;

    public BaseRecyclerViewLibAdapter(Context context, List<D> data) {
        this.context = context;
        if (null != data) {
            this.data = data;
        } else {
            this.data = new ArrayList<>();
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        T viewDelegate = initDelegate(context);
        View convertView = viewDelegate.getRootView();
        BaseViewHolder baseViewHolder = new BaseViewHolder(convertView);
        baseViewHolder.setBaseAdapterDelegate(viewDelegate);
        return baseViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        T viewDelegate = null;
        try {
            viewDelegate = (T)holder.getBaseAdapterDelegate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (null != getDataBinder(position) && null != getItemData(position)) {
                Object obj = null;
                try {
                    obj = BCObjectProcessingUtil.objMandatoryConversionClass(getItemData(position),getDataBinder(position).getDataClass());
                } catch (Exception e) {
                    e.printStackTrace();
                    obj=null;
                }
                if(null!=obj){
                    getDataBinder(position).viewBindModel(viewDelegate, getItemData(position));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LKLogUtil.e(e.toString(),e);
        }
        notifyView(viewDelegate, position);
        bindEvenListener(viewDelegate, position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    /** 注册界面控件点击事件*/
    protected abstract void bindEvenListener(T viewDelegate, int position);

    /** 自定义实现View刷新操作，通常用于单个的刷新或者未进行数据绑定时使用，如已经进行过数据绑定，则无需具体实现此方法*/
    protected abstract void notifyView(T viewDelegate, int position);

    /** 指定界面的视图控制器，必须指定*/
    public abstract Class<T> getDelegateClass();

    /** 公共数据视图绑定控制器数据对象*/
    public abstract BaseDataBinder getDataBinder(int position);

    /** 获取Item数据*/
    public abstract Object getItemData(int position);

    /** 获取Item类型*/
    public abstract Integer getItemType(int position);

    public List<D> getData() {
        return data;
    }

    public void setData(List<D> data) {
        this.data = data;
    }

    public Context getContext() {
        return context;
    }

    /** 初始化视图托管类对象*/
    public T initDelegate(Context context){
        T viewDelegate;
        try {
            viewDelegate = getDelegateClass().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("create IDelegate error");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create IDelegate error");
        }
        if(null!=context){
            viewDelegate.create(LayoutInflater.from(context),null,null);
        }
//        if(null == binder){
//            binder = getDataBinder();
//        }
        return viewDelegate;
    }

    @Override
    public int getItemViewType(int position) {
        return getItemType(position);
    }
}
